﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.RendirViajes
{
    public partial class RendierViajes : Form
    {
        public RendierViajes()
        {
            InitializeComponent();
            tb_chofer_index.Visible = tb_turno_index.Visible = false;
            tb_importe.Enabled = false;
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NUNCA_TAXI.Choferes WHERE Ch_Habilitado = 1", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cb_chofer.Items.Add("(" + dt.Rows[i]["Id_Chofer"].ToString() + ") (" + dt.Rows[i]["Ch_Nombre"].ToString() + ") (" + dt.Rows[i]["Ch_Apellido"].ToString() + ") (" + dt.Rows[i]["Ch_Dni"].ToString() + ")");
                }
                da = new SqlDataAdapter("SELECT * FROM NUNCA_TAXI.Turnos WHERE Tu_Habilitado = 1", con);
                dt = new DataTable();
                da.Fill(dt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    cb_turno.Items.Add(dt.Rows[i]["Tu_Descripcion"].ToString());
                }
            }
        }

        private void RendierViajes_Load(object sender, EventArgs e)
        {

        }

        private void cb_chofer_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] split = cb_chofer.SelectedIndex.ToString().Split();
            string indice = split[0];
            int numero = Int32.Parse(indice) + 1;
            tb_chofer_index.Text = numero.ToString();
        }

        private void cb_turno_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] split = cb_turno.SelectedIndex.ToString().Split();
            string indice = split[0];
            int numero = Int32.Parse(indice) + 1;
            tb_turno_index.Text = numero.ToString();
        }

        private void calcular_Click(object sender, EventArgs e)
        {
            if(tb_turno_index.Text != "" && tb_chofer_index.Text != ""){
                using (SqlConnection con = Modelo.Modelo.createConnection())
                {
                    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NUNCA_TAXI.Viajes WHERE (Vi_FechaRendido IS NULL) AND (Id_Turno = " + tb_turno_index.Text + ") AND (Id_Chofer = " + tb_chofer_index.Text + ")", con);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;

                    da = new SqlDataAdapter("SELECT Id_Chofer, SUM(Vi_Importe) AS Monto_Rendicion FROM NUNCA_TAXI.Viajes WHERE (Vi_FechaRendido IS NULL) AND (Id_Turno = " + tb_turno_index.Text + ") AND (Id_Chofer = " + tb_chofer_index.Text + ") GROUP BY Id_Chofer", con);
                    dt = new DataTable();
                    da.Fill(dt);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("No hay elementos a mostrar");
                    }
                    else
                    {
                        tb_importe.Text = dt.Rows[0]["Monto_Rendicion"].ToString();
                    }
                }
            }
        }

        private void rendir_Click(object sender, EventArgs e)
        {
            if (tb_turno_index.Text != "" && tb_chofer_index.Text != "")
            {
                using (SqlConnection con = Modelo.Modelo.createConnection())
                {
                    SqlCommand comando = new SqlCommand("UPDATE NUNCA_TAXI.Viajes SET Vi_FechaRendido=@fecha WHERE (Vi_FechaRendido IS NULL) AND (Id_Turno = " + tb_turno_index.Text + ") AND (Id_Chofer = " + tb_chofer_index.Text + ")", con);
                    comando.Parameters.AddWithValue("fecha", dt_fecha.Value);

                    try
                    {
                        Modelo.Modelo.openConnection(con);
                        comando.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        Modelo.Modelo.closeConnection(con);
                    }

                    comando = new SqlCommand("INSERT INTO NUNCA_TAXI.Rendiciones(Id_Chofer,Id_Turno,Ren_Fecha,Ren_Importe) VALUES(@chofer,@turno,@fecha,@importe)", con);
                    comando.Parameters.AddWithValue("chofer", tb_chofer_index.Text);
                    comando.Parameters.AddWithValue("turno", tb_turno_index.Text);
                    comando.Parameters.AddWithValue("fecha", dt_fecha.Value);
                    comando.Parameters.AddWithValue("importe", Convert.ToDecimal(tb_importe.Text));

                    try
                    {
                        Modelo.Modelo.openConnection(con);
                        comando.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        cb_turno.SelectedItem = -1;
                        cb_chofer.SelectedIndex = -1;
                        tb_importe.Text = "";

                        dt_fecha.Value = System.DateTime.Now;
                        Modelo.Modelo.closeConnection(con);
                    }
                }
            }
        }
    }
}
